package com.xiaohei.activiti;

import com.xiaohei.ServiceAuthApplication;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: ProcessTestDemo
 * Package:
 * Description:
 *
 * @Author:xiaohei
 * @Create 2024/5/3 20:59
 * Version 1.0
 */
@ExtendWith(SpringExtension.class)  //集成Spring测试框架  扩展测试执行  提供上下文信息
@SpringBootTest(classes = ServiceAuthApplication.class)
public class ProcessTestDemo {
    //注入RepositoryService
    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    //部署流程定义
    @Test
    public void deployProcess(){
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("process/jiaban.bpmn20.xml")
                .name("加班申请流程")
                .deploy();
        System.out.println(deployment.getId());
        System.out.println(deployment.getName());
    }

    //启动流程实例
    @Test
    public void startProcessInstance(){
        Map<String,Object> map = new HashMap<String,Object>();
        //设置任务人
        map.put("assgin1","lucy");
        map.put("assgin2","mary");

        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaban", map);

        System.out.println(processInstance.getProcessDefinitionId());
        System.out.println(processInstance.getParentProcessInstanceId());
    }

    @Test
    public void findTaskList(){
        List<Task> list = taskService.createTaskQuery().taskAssignee("lucy").list();
        for(Task task : list){
            System.out.println(task.getProcessInstanceId());
            System.out.println(task.getId());
            System.out.println(task.getAssignee());
            System.out.println(task.getName());
        }
    }
}
